package com.myh.cc.base


import android.content.Context
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.lang.reflect.ParameterizedType

/**
 * 类描述    ：
 * 创建人    ：  mayahao.
 * 创建时间   ：  2018/1/7.
 * 修改人    ：
 * 修改时间   ：
 * 修改备注   ：
 */
abstract class BaseFragment<T: ViewDataBinding,C :IBaseControl ,M: BaseModel<T,C>>: Fragment(),IModeActivity<T> {


    lateinit var mBinder: T

    lateinit var mModel: M

    abstract fun getLayoutId(): Int


    abstract fun initView()



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val genType = javaClass.genericSuperclass
        val params = (genType as ParameterizedType).actualTypeArguments
        val bizClass = params[2] as Class<*>
        try {
            mModel = bizClass.newInstance() as M
        } catch (e: java.lang.InstantiationException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }


    }



    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mBinder = DataBindingUtil.inflate(LayoutInflater.from(activity), getLayoutId(), null, false)

        // 初始化 ViewModel
        mModel.setView(this)
        //同步Activity 生命周期
        mModel.onCreate()
        initView()
        return mBinder.root
    }


    override fun getContext(): Context {

       return  super<Fragment>.getContext()!!
    }

    override fun getBinder(): T {

        return  mBinder
    }


    override fun onStart() {
        super.onStart()
        mModel.onStart()
    }


    override fun onResume() {
        super.onResume()
        mModel.onResume()
    }

    override fun onStop() {
        super.onStop()
        mModel.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        mModel.onDestroy()
    }





    override fun showLoadingDialog() {

    }

    override fun hideLoadingDialog() {

    }
}